﻿using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;

namespace Sidvall.LogManager.Web.Controllers.Api.V1
{
    public partial class LogScopeItemController : ApiController
    {
        #region NewItem

        partial void OnBeforeNewItem(Sidvall.Business.DataPortalContext<global::Sidvall.LogManager.Data.ILogScopeItem, global::Sidvall.Data.CreateCriteria> dataPortalContext);
        partial void OnAfterNewItem(Sidvall.Business.DataPortalContext<global::Sidvall.LogManager.Data.ILogScopeItem, global::Sidvall.Data.CreateCriteria> dataPortalContext);
        [HttpPost]
        [Route("api/V1/LogScopeItem/NewItem")]
        public async Task<HttpResponseMessage> NewItem([FromBody]global::Sidvall.Data.CreateCriteria criteria)
        {
            HttpResponseMessage message;
            Sidvall.Business.DataPortalContext<global::Sidvall.LogManager.Data.ILogScopeItem, global::Sidvall.Data.CreateCriteria> dataPortalContext;

            if (criteria == null)
                throw new System.ArgumentNullException(nameof(criteria));
            dataPortalContext = Sidvall.Business.DataPortalManager.NewContext(global::Sidvall.LogManager.Data.Model.DomainObjects.LogScopeItem, Sidvall.Security.Operations.NewItems, null, criteria, (global::Sidvall.LogManager.Data.ILogScopeItem)null);
            do
            {
                try
                {
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.EnsureIsAuthorizedAsync(this, dataPortalContext);
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.LogAsync(dataPortalContext);
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalInvoke(dataPortalContext);
                    OnBeforeNewItem(dataPortalContext);
                    await dataPortalContext.DoTaskAsync();
                    if (dataPortalContext.Result == Sidvall.Business.DataPortalResult.None)
                        dataPortalContext.ResultValueItem = await global::Sidvall.LogManager.SystemContext.Current.DataPortal.LogScopeItemManager.NewItemAsync(dataPortalContext);
                    OnAfterNewItem(dataPortalContext);
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalInvokeComplete(dataPortalContext);
                    await dataPortalContext.DoTaskAsync();
                    if (dataPortalContext.ResultValueItem != null)
                        message = this.Request.CreateResponse(HttpStatusCode.OK, (global::Sidvall.LogManager.Net.Services.V1.LogScopeItem)dataPortalContext.ResultValueItem);
                    else
                        message = this.Request.CreateResponse(HttpStatusCode.NoContent);
                }
                catch (System.Exception ex)
                {
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.LogAsync(ex);
                    message = Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.GetResponseMessage(this, ex);
                    dataPortalContext.Exception = ex;
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalException(dataPortalContext);
                    await dataPortalContext.DoTaskAsync(false);
                }
            } while (dataPortalContext.DoRetry());
            return message;
        }

        #endregion
        #region NewItems

        partial void OnBeforeNewItems(Sidvall.Business.DataPortalContext<global::Sidvall.LogManager.Data.ILogScopeItem, global::Sidvall.Data.CreateCriteria> dataPortalContext);
        partial void OnAfterNewItems(Sidvall.Business.DataPortalContext<global::Sidvall.LogManager.Data.ILogScopeItem, global::Sidvall.Data.CreateCriteria> dataPortalContext);
        [HttpPost]
        [Route("api/V1/LogScopeItem/NewItems")]
        public async Task<HttpResponseMessage> NewItems([FromBody]global::Sidvall.Data.CreateCriteria criteria)
        {
            HttpResponseMessage message;
            Sidvall.Business.DataPortalContext<global::Sidvall.LogManager.Data.ILogScopeItem, global::Sidvall.Data.CreateCriteria> dataPortalContext;

            if (criteria == null)
                throw new System.ArgumentNullException(nameof(criteria));
            dataPortalContext = Sidvall.Business.DataPortalManager.NewContext(global::Sidvall.LogManager.Data.Model.DomainObjects.LogScopeItem, Sidvall.Security.Operations.NewItems, null, criteria, (global::Sidvall.LogManager.Data.ILogScopeItem)null);
            do
            {
                try
                {
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.EnsureIsAuthorizedAsync(this, dataPortalContext);
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.LogAsync(dataPortalContext);
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalInvoke(dataPortalContext);
                    OnBeforeNewItems(dataPortalContext);
                    await dataPortalContext.DoTaskAsync();
                    if (dataPortalContext.Result == Sidvall.Business.DataPortalResult.None)
                        dataPortalContext.ResultValueListProvider = await global::Sidvall.LogManager.SystemContext.Current.DataPortal.LogScopeItemManager.NewItemsAsync(dataPortalContext);
                    OnAfterNewItems(dataPortalContext);
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalInvokeComplete(dataPortalContext);
                    await dataPortalContext.DoTaskAsync();
                    if (dataPortalContext.ResultValueListProvider != null)
                        message = this.Request.CreateResponse(HttpStatusCode.OK, (global::Sidvall.LogManager.Net.Services.V1.LogScopeItemCollectionItem)dataPortalContext.ResultValueListProvider);
                    else
                        message = this.Request.CreateResponse(HttpStatusCode.NoContent);
                }
                catch (System.Exception ex)
                {
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.LogAsync(ex);
                    message = Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.GetResponseMessage(this, ex);
                    dataPortalContext.Exception = ex;
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalException(dataPortalContext);
                    await dataPortalContext.DoTaskAsync(false);
                }
            } while (dataPortalContext.DoRetry());
            return message;
        }

        #endregion
        #region GetItem

        partial void OnBeforeGetItem(Sidvall.Business.DataPortalContext<global::Sidvall.LogManager.Data.ILogScopeItem, global::Sidvall.Data.FetchCriteria<global::Sidvall.LogManager.Data.Fields.LogScopeItemField, global::Sidvall.LogManager.Data.Relations.LogScopeItemRelation>> dataPortalContext);
        partial void OnAfterGetItem(Sidvall.Business.DataPortalContext<global::Sidvall.LogManager.Data.ILogScopeItem, global::Sidvall.Data.FetchCriteria<global::Sidvall.LogManager.Data.Fields.LogScopeItemField, global::Sidvall.LogManager.Data.Relations.LogScopeItemRelation>> dataPortalContext);
        [HttpPost]
        [Route("api/V1/LogScopeItem/GetItem")]
        public async Task<HttpResponseMessage> GetItem([FromBody]global::Sidvall.Data.FetchCriteria<global::Sidvall.LogManager.Data.Fields.LogScopeItemField, global::Sidvall.LogManager.Data.Relations.LogScopeItemRelation> criteria)
        {
            HttpResponseMessage message;
            Sidvall.Business.DataPortalContext<global::Sidvall.LogManager.Data.ILogScopeItem, global::Sidvall.Data.FetchCriteria<global::Sidvall.LogManager.Data.Fields.LogScopeItemField, global::Sidvall.LogManager.Data.Relations.LogScopeItemRelation>> dataPortalContext;

            if (criteria == null)
                throw new System.ArgumentNullException(nameof(criteria));
            dataPortalContext = Sidvall.Business.DataPortalManager.NewContext(global::Sidvall.LogManager.Data.Model.DomainObjects.LogScopeItem, Sidvall.Security.Operations.GetItems, null, criteria, 
                (global::Sidvall.LogManager.Data.ILogScopeItem)null);
            do
            {
                try
                {
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.EnsureIsAuthorizedAsync(this, dataPortalContext);
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.LogAsync(dataPortalContext);
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalInvoke(dataPortalContext);
                    OnBeforeGetItem(dataPortalContext);
                    await dataPortalContext.DoTaskAsync();
                    if (dataPortalContext.Result == Sidvall.Business.DataPortalResult.None)
                        dataPortalContext.ResultValueItem = await global::Sidvall.LogManager.SystemContext.Current.DataPortal.LogScopeItemManager.GetItemAsync(dataPortalContext);
                    OnAfterGetItem(dataPortalContext);
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalInvokeComplete(dataPortalContext);
                    await dataPortalContext.DoTaskAsync();
                    if (dataPortalContext.ResultValueItem != null)
                        message = this.Request.CreateResponse(HttpStatusCode.OK, (global::Sidvall.LogManager.Net.Services.V1.LogScopeItem)dataPortalContext.ResultValueItem);
                    else
                        message = this.Request.CreateResponse(HttpStatusCode.NoContent);
                }
                catch (System.Exception ex)
                {
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.LogAsync(ex);
                    message = Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.GetResponseMessage(this, ex);
                    dataPortalContext.Exception = ex;
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalException(dataPortalContext);
                    await dataPortalContext.DoTaskAsync(false);
                }
            } while (dataPortalContext.DoRetry());
            return message;
        }

        #endregion
        #region GetItems

        partial void OnBeforeGetItems(Sidvall.Business.DataPortalContext<global::Sidvall.LogManager.Data.ILogScopeItem, global::Sidvall.Data.FetchListCriteria<global::Sidvall.LogManager.Data.Fields.LogScopeItemField, global::Sidvall.LogManager.Data.Relations.LogScopeItemRelation>> dataPortalContext);
        partial void OnAfterGetItems(Sidvall.Business.DataPortalContext<global::Sidvall.LogManager.Data.ILogScopeItem, global::Sidvall.Data.FetchListCriteria<global::Sidvall.LogManager.Data.Fields.LogScopeItemField, global::Sidvall.LogManager.Data.Relations.LogScopeItemRelation>> dataPortalContext);
        [HttpPost]
        [Route("api/V1/LogScopeItem/GetItems")]
        public async Task<HttpResponseMessage> GetItems([FromBody]global::Sidvall.Data.FetchListCriteria<global::Sidvall.LogManager.Data.Fields.LogScopeItemField, global::Sidvall.LogManager.Data.Relations.LogScopeItemRelation> criteria)
        {
            HttpResponseMessage message;
            Sidvall.Business.DataPortalContext<global::Sidvall.LogManager.Data.ILogScopeItem, global::Sidvall.Data.FetchListCriteria<global::Sidvall.LogManager.Data.Fields.LogScopeItemField, global::Sidvall.LogManager.Data.Relations.LogScopeItemRelation>> dataPortalContext;

            if (criteria == null)
                throw new System.ArgumentNullException(nameof(criteria));
            dataPortalContext = Sidvall.Business.DataPortalManager.NewContext(global::Sidvall.LogManager.Data.Model.DomainObjects.LogScopeItem, Sidvall.Security.Operations.GetItems, null, criteria, 
                (global::Sidvall.LogManager.Data.ILogScopeItem)null);
            do
            {
                try
                {
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.EnsureIsAuthorizedAsync(this, dataPortalContext);
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.LogAsync(dataPortalContext);
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalInvoke(dataPortalContext);
                    OnBeforeGetItems(dataPortalContext);
                    await dataPortalContext.DoTaskAsync();
                    if (dataPortalContext.Result == Sidvall.Business.DataPortalResult.None)
                        dataPortalContext.ResultValueListProvider = await global::Sidvall.LogManager.SystemContext.Current.DataPortal.LogScopeItemManager.GetItemsAsync(dataPortalContext);
                    OnAfterGetItems(dataPortalContext);
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalInvokeComplete(dataPortalContext);
                    await dataPortalContext.DoTaskAsync();
                    if (dataPortalContext.ResultValueListProvider != null)
                        message = this.Request.CreateResponse(HttpStatusCode.OK, (global::Sidvall.LogManager.Net.Services.V1.LogScopeItemCollectionItem)dataPortalContext.ResultValueListProvider);
                    else
                        message = this.Request.CreateResponse(HttpStatusCode.NoContent);
                }
                catch (System.Exception ex)
                {
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.LogAsync(ex);
                    message = Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.GetResponseMessage(this, ex);
                    dataPortalContext.Exception = ex;
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalException(dataPortalContext);
                    await dataPortalContext.DoTaskAsync(false);
                }
            } while (dataPortalContext.DoRetry());
            return message;
        }

        #endregion
        #region GetItemCount

        partial void OnBeforeGetItemCount(Sidvall.Business.DataPortalContext<long, global::Sidvall.Data.FetchCountCriteria<global::Sidvall.LogManager.Data.Fields.LogScopeItemField>> dataPortalContext);
        partial void OnAfterGetItemCount(Sidvall.Business.DataPortalContext<long, global::Sidvall.Data.FetchCountCriteria<global::Sidvall.LogManager.Data.Fields.LogScopeItemField>> dataPortalContext);
        [HttpPost]
        [Route("api/V1/LogScopeItem/GetItemCount")]
        public async Task<HttpResponseMessage> GetItemCount([FromBody]global::Sidvall.Data.FetchCountCriteria<global::Sidvall.LogManager.Data.Fields.LogScopeItemField> criteria)
        {
            HttpResponseMessage message;
            Sidvall.Business.DataPortalContext<long, global::Sidvall.Data.FetchCountCriteria<global::Sidvall.LogManager.Data.Fields.LogScopeItemField>> dataPortalContext;

            if (criteria == null)
                throw new System.ArgumentNullException(nameof(criteria));
            dataPortalContext = Sidvall.Business.DataPortalManager.NewContext(global::Sidvall.LogManager.Data.Model.DomainObjects.LogScopeItem, Sidvall.Security.Operations.GetItemsMetadata, null, criteria, 0L);
            do
            {
                try
                {
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.EnsureIsAuthorizedAsync(this, dataPortalContext);
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.LogAsync(dataPortalContext);
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalInvoke(dataPortalContext);
                    OnBeforeGetItemCount(dataPortalContext);
                    await dataPortalContext.DoTaskAsync();
                    if (dataPortalContext.Result == Sidvall.Business.DataPortalResult.None)
                        dataPortalContext.ResultValueItem = await global::Sidvall.LogManager.SystemContext.Current.DataPortal.LogScopeItemManager.GetItemCountAsync(dataPortalContext);
                    OnAfterGetItemCount(dataPortalContext);
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalInvokeComplete(dataPortalContext);
                    await dataPortalContext.DoTaskAsync();
                    message = this.Request.CreateResponse(HttpStatusCode.OK, dataPortalContext.ResultValueItem);
                }
                catch (System.Exception ex)
                {
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.LogAsync(ex);
                    message = Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.GetResponseMessage(this, ex);
                    dataPortalContext.Exception = ex;
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalException(dataPortalContext);
                    await dataPortalContext.DoTaskAsync(false);
                }
            } while (dataPortalContext.DoRetry());
            return message;
        }

        #endregion
        #region DeleteItems

        partial void OnBeforeDelete(Sidvall.Business.DataPortalContext<int, global::Sidvall.Data.DeleteCriteria<global::Sidvall.LogManager.Data.Fields.LogScopeItemField>> dataPortalContext);
        partial void OnAfterDelete(Sidvall.Business.DataPortalContext<int, global::Sidvall.Data.DeleteCriteria<global::Sidvall.LogManager.Data.Fields.LogScopeItemField>> dataPortalContext);
        [HttpPost]
        [Route("api/V1/LogScopeItem/DeleteItems")]
        public async Task<HttpResponseMessage> DeleteItems([FromBody]global::Sidvall.Data.DeleteCriteria<global::Sidvall.LogManager.Data.Fields.LogScopeItemField> criteria)
        {
            HttpResponseMessage message;
            Sidvall.Business.DataPortalContext<int, global::Sidvall.Data.DeleteCriteria<global::Sidvall.LogManager.Data.Fields.LogScopeItemField>> dataPortalContext;

            if (criteria == null)
                throw new System.ArgumentNullException(nameof(criteria));
            dataPortalContext = Sidvall.Business.DataPortalManager.NewContext(global::Sidvall.LogManager.Data.Model.DomainObjects.LogScopeItem, Sidvall.Security.Operations.DeleteItems, null, criteria, 0);
            do
            {
                try
                {
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.EnsureIsAuthorizedAsync(this, dataPortalContext);
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.LogAsync(dataPortalContext);
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalInvoke(dataPortalContext);
                    OnBeforeDelete(dataPortalContext);
                    await dataPortalContext.DoTaskAsync();
                    if (dataPortalContext.Result == Sidvall.Business.DataPortalResult.None)
                        dataPortalContext.ResultValueItem = await global::Sidvall.LogManager.SystemContext.Current.DataPortal.LogScopeItemManager.DeleteItemsAsync(dataPortalContext);
                    OnAfterDelete(dataPortalContext);
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalInvokeComplete(dataPortalContext);
                    await dataPortalContext.DoTaskAsync();
                    if (dataPortalContext.ResultValueItem > 0)
                        message = this.Request.CreateResponse(HttpStatusCode.OK, dataPortalContext.ResultValueItem);
                    else
                        message = this.Request.CreateResponse(HttpStatusCode.NoContent);
                }
                catch (System.Exception ex)
                {
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.LogAsync(ex);
                    message = Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.GetResponseMessage(this, ex);
                    dataPortalContext.Exception = ex;
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalException(dataPortalContext);
                    await dataPortalContext.DoTaskAsync(false);
                }
            } while (dataPortalContext.DoRetry());
            return message;
        }

        #endregion
        #region SaveItem

        partial void OnBeforeSaveItem(Sidvall.Data.SaveContext<global::Sidvall.LogManager.Net.Services.V1.LogScopeItem> context);
        partial void OnBeforeSaveItem(Sidvall.Business.DataPortalContext<global::Sidvall.LogManager.Data.ILogScopeItem, Sidvall.Data.SaveContext<global::Sidvall.LogManager.Data.ILogScopeItem>> dataPortalContext);
        partial void OnAfterSaveItem(Sidvall.Business.DataPortalContext<global::Sidvall.LogManager.Data.ILogScopeItem, Sidvall.Data.SaveContext<global::Sidvall.LogManager.Data.ILogScopeItem>> dataPortalContext);
        [HttpPost]
        [Route("api/V1/LogScopeItem/SaveItem")]
        public async Task<HttpResponseMessage> SaveItem([FromBody]Sidvall.Data.SaveContext<global::Sidvall.LogManager.Net.Services.V1.LogScopeItem> context)
        {
            HttpResponseMessage message;
            Sidvall.Business.DataPortalContext<global::Sidvall.LogManager.Data.ILogScopeItem, Sidvall.Data.SaveContext<global::Sidvall.LogManager.Data.ILogScopeItem>> dataPortalContext;

            if (context == null)
                throw new System.ArgumentNullException(nameof(context));
            OnBeforeSaveItem(context);
            var saveContext = new Sidvall.Data.SaveContext<global::Sidvall.LogManager.Data.ILogScopeItem>()
            {
                Item = context.Item,
                Settings = context.Settings,
            };
            dataPortalContext = Sidvall.Business.DataPortalManager.NewSaveItemContext(global::Sidvall.LogManager.Data.Model.DomainObjects.LogScopeItem, null, saveContext);
            dataPortalContext.AcceptChangesSettings = new Sidvall.Data.AcceptChangesSettings(Sidvall.Data.DataRowContextOption.State);
            dataPortalContext.CopyOnSave = false;
            do
            {
                try
                {
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.EnsureIsAuthorizedAsync(this, dataPortalContext);
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.LogAsync(dataPortalContext);
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalInvoke(dataPortalContext);
                    OnBeforeSaveItem(dataPortalContext);
                    await dataPortalContext.DoTaskAsync();
                    if (dataPortalContext.Result == Sidvall.Business.DataPortalResult.None)
                        dataPortalContext.ResultValueItem = await global::Sidvall.LogManager.SystemContext.Current.DataPortal.LogScopeItemManager.SaveItemAsync(dataPortalContext);
                    OnAfterSaveItem(dataPortalContext);
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalInvokeComplete(dataPortalContext);
                    await dataPortalContext.DoTaskAsync();
                    if (dataPortalContext.ResultValueItem != null)
                        message = this.Request.CreateResponse(HttpStatusCode.OK, (global::Sidvall.LogManager.Net.Services.V1.LogScopeItem)dataPortalContext.ResultValueItem);
                    else
                        message = this.Request.CreateResponse(HttpStatusCode.NoContent);
                }
                catch (System.Exception ex)
                {
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.LogAsync(ex);
                    message = Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.GetResponseMessage(this, ex);
                    dataPortalContext.Exception = ex;
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalException(dataPortalContext);
                    await dataPortalContext.DoTaskAsync(false);
                }
            } while (dataPortalContext.DoRetry());
            return message;
        }

        #endregion
        #region SaveItems

        partial void OnBeforeSaveItems(Sidvall.Data.SaveContext<global::Sidvall.LogManager.Net.Services.V1.LogScopeItemCollectionItem> context);
        partial void OnBeforeSaveItems(Sidvall.Business.DataPortalContext<global::Sidvall.LogManager.Data.ILogScopeItem, Sidvall.Data.SaveContext<Sidvall.Data.IDataRowProvider<global::Sidvall.LogManager.Data.ILogScopeItem>>> dataPortalContext);
        partial void OnAfterSaveItems(Sidvall.Business.DataPortalContext<global::Sidvall.LogManager.Data.ILogScopeItem, Sidvall.Data.SaveContext<Sidvall.Data.IDataRowProvider<global::Sidvall.LogManager.Data.ILogScopeItem>>> dataPortalContext);
        [HttpPost]
        [Route("api/V1/LogScopeItem/SaveItems")]
        public async Task<HttpResponseMessage> SaveItems([FromBody]Sidvall.Data.SaveContext<global::Sidvall.LogManager.Net.Services.V1.LogScopeItemCollectionItem> context)
        {
            HttpResponseMessage message;
            Sidvall.Business.DataPortalContext<global::Sidvall.LogManager.Data.ILogScopeItem, Sidvall.Data.SaveContext<Sidvall.Data.IDataRowProvider<global::Sidvall.LogManager.Data.ILogScopeItem>>> dataPortalContext;

            if (context == null)
                throw new System.ArgumentNullException(nameof(context));
            OnBeforeSaveItems(context);
            var saveContext = new Sidvall.Data.SaveContext<Sidvall.Data.IDataRowProvider<global::Sidvall.LogManager.Data.ILogScopeItem>>()
            {
                Item = context.Item,
                Settings = context.Settings,
            };
            dataPortalContext = Sidvall.Business.DataPortalManager.NewSaveItemsContext(global::Sidvall.LogManager.Data.Model.DomainObjects.LogScopeItem, null, saveContext);
            dataPortalContext.AcceptChangesSettings = new Sidvall.Data.AcceptChangesSettings(Sidvall.Data.DataRowContextOption.State);
            dataPortalContext.CopyOnSave = false;
            do
            {
                try
                {
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.EnsureIsAuthorizedAsync(this, dataPortalContext);
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.LogAsync(dataPortalContext);
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalInvoke(dataPortalContext);
                    OnBeforeSaveItems(dataPortalContext);
                    await dataPortalContext.DoTaskAsync();
                    if (dataPortalContext.Result == Sidvall.Business.DataPortalResult.None)
                        dataPortalContext.ResultValueListProvider = await global::Sidvall.LogManager.SystemContext.Current.DataPortal.LogScopeItemManager.SaveItemsAsync(dataPortalContext);
                    OnAfterSaveItems(dataPortalContext);
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalInvokeComplete(dataPortalContext);
                    await dataPortalContext.DoTaskAsync();
                    if (dataPortalContext.ResultValueListProvider != null)
                        message = this.Request.CreateResponse(HttpStatusCode.OK, (global::Sidvall.LogManager.Net.Services.V1.LogScopeItemCollectionItem)dataPortalContext.ResultValueListProvider);
                    else
                        message = this.Request.CreateResponse(HttpStatusCode.NoContent);
                }
                catch (System.Exception ex)
                {
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.LogAsync(ex);
                    message = Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.GetResponseMessage(this, ex);
                    dataPortalContext.Exception = ex;
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalException(dataPortalContext);
                    await dataPortalContext.DoTaskAsync(false);
                }
            } while (dataPortalContext.DoRetry());
            return message;
        }

        #endregion
        #region UpdateItemValues

        partial void OnBeforeUpdateItemValues(Sidvall.Business.DataPortalContext<bool, Sidvall.Data.UpdateItemContext<global::Sidvall.LogManager.Data.Fields.LogScopeItemField>> dataPortalContext);
        partial void OnAfterUpdateItemValues(Sidvall.Business.DataPortalContext<bool, Sidvall.Data.UpdateItemContext<global::Sidvall.LogManager.Data.Fields.LogScopeItemField>> dataPortalContext);
        [HttpPost]
        [Route("api/V1/LogScopeItem/UpdateItemValues")]
        public async Task<HttpResponseMessage> UpdateItemValues([FromBody]Sidvall.Data.UpdateItemContext<global::Sidvall.LogManager.Data.Fields.LogScopeItemField> criteria)
        {
            HttpResponseMessage message;

            if (criteria == null)
                throw new System.ArgumentNullException(nameof(criteria));
            var dataPortalContext = Sidvall.Business.DataPortalManager.NewContext(global::Sidvall.LogManager.Data.Model.DomainObjects.LogScopeItem, Sidvall.Security.Operations.UpdateItemValues, null, criteria, false);
            do
            {
                try
                {
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.EnsureIsAuthorizedAsync(this, dataPortalContext);
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.LogAsync(dataPortalContext);
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalInvoke(dataPortalContext);
                    OnBeforeUpdateItemValues(dataPortalContext);
                    await dataPortalContext.DoTaskAsync();
                    if (dataPortalContext.Result == Sidvall.Business.DataPortalResult.None)
                        dataPortalContext.ResultValueItem = await global::Sidvall.LogManager.SystemContext.Current.DataPortal.LogScopeItemManager.UpdateItemAsync(dataPortalContext);
                    OnAfterUpdateItemValues(dataPortalContext);
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalInvokeComplete(dataPortalContext);
                    await dataPortalContext.DoTaskAsync();
                    if (dataPortalContext.ResultValueItem)
                        message = this.Request.CreateResponse(HttpStatusCode.OK, dataPortalContext.ResultValueItem);
                    else
                        message = this.Request.CreateResponse(HttpStatusCode.NoContent);
                }
                catch (System.Exception ex)
                {
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.LogAsync(ex);
                    message = Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.GetResponseMessage(this, ex);
                    dataPortalContext.Exception = ex;
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalException(dataPortalContext);
                    await dataPortalContext.DoTaskAsync(false);
                }
            } while (dataPortalContext.DoRetry());
            return message;
        }

        #endregion
        #region UpdateItemsValues

        partial void OnBeforeUpdateItemsValues(Sidvall.Business.DataPortalContext<int, Sidvall.Data.UpdateItemsContext<global::Sidvall.LogManager.Data.Fields.LogScopeItemField>> dataPortalContext);
        partial void OnAfterUpdateItemsValues(Sidvall.Business.DataPortalContext<int, Sidvall.Data.UpdateItemsContext<global::Sidvall.LogManager.Data.Fields.LogScopeItemField>> dataPortalContext);
        [HttpPost]
        [Route("api/V1/LogScopeItem/UpdateItemsValues")]
        public async Task<HttpResponseMessage> UpdateItemsValues([FromBody]Sidvall.Data.UpdateItemsContext<global::Sidvall.LogManager.Data.Fields.LogScopeItemField> criteria)
        {
            HttpResponseMessage message;
            Sidvall.Business.DataPortalContext<int, Sidvall.Data.UpdateItemsContext<global::Sidvall.LogManager.Data.Fields.LogScopeItemField>> dataPortalContext;

            if (criteria == null)
                throw new System.ArgumentNullException(nameof(criteria));
            dataPortalContext = Sidvall.Business.DataPortalManager.NewContext(global::Sidvall.LogManager.Data.Model.DomainObjects.LogScopeItem, Sidvall.Security.Operations.UpdateItemValues, null, criteria, 0);
            do
            {
                try
                {
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.EnsureIsAuthorizedAsync(this, dataPortalContext);
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.LogAsync(dataPortalContext);
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalInvoke(dataPortalContext);
                    OnBeforeUpdateItemsValues(dataPortalContext);
                    await dataPortalContext.DoTaskAsync();
                    if (dataPortalContext.Result == Sidvall.Business.DataPortalResult.None)
                        dataPortalContext.ResultValueItem = await global::Sidvall.LogManager.SystemContext.Current.DataPortal.LogScopeItemManager.UpdateItemsAsync(dataPortalContext);
                    OnAfterUpdateItemsValues(dataPortalContext);
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalInvokeComplete(dataPortalContext);
                    await dataPortalContext.DoTaskAsync();
                    if (dataPortalContext.ResultValueItem > 0)
                        message = this.Request.CreateResponse(HttpStatusCode.OK, dataPortalContext.ResultValueItem);
                    else
                        message = this.Request.CreateResponse(HttpStatusCode.NoContent);
                }
                catch (System.Exception ex)
                {
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.LogAsync(ex);
                    message = Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.GetResponseMessage(this, ex);
                    dataPortalContext.Exception = ex;
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalException(dataPortalContext);
                    await dataPortalContext.DoTaskAsync(false);
                }
            } while (dataPortalContext.DoRetry());
            return message;
        }

        #endregion
        #region Execute

        partial void OnBeforeExecute(Sidvall.Business.DataPortalContext<global::Sidvall.Data.CommandCriteria, global::Sidvall.Data.CommandCriteria> dataPortalContext);
        partial void OnAfterExecute(Sidvall.Business.DataPortalContext<global::Sidvall.Data.CommandCriteria, global::Sidvall.Data.CommandCriteria> dataPortalContext);
        [HttpPost]
        [Route("api/V1/LogScopeItem/Execute")]
        public async Task<HttpResponseMessage> Execute([FromBody]global::Sidvall.Data.CommandCriteria criteria)
        {
            HttpResponseMessage message;
            global::Sidvall.Business.DataPortalContext<global::Sidvall.Data.CommandCriteria, global::Sidvall.Data.CommandCriteria> dataPortalContext;

            if (criteria == null)
                throw new System.ArgumentNullException(nameof(criteria));
            dataPortalContext = Sidvall.Business.DataPortalManager.NewContext(global::Sidvall.LogManager.Data.Model.DomainObjects.LogScopeItem, Sidvall.Security.Operations.Execute, null, criteria);
            do
            {
                try
                {
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.EnsureIsAuthorizedAsync(this, dataPortalContext);
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.LogAsync(dataPortalContext);
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalInvoke(dataPortalContext);
                    OnBeforeExecute(dataPortalContext);
                    await dataPortalContext.DoTaskAsync();
                    if (dataPortalContext.Result == Sidvall.Business.DataPortalResult.None)
                        dataPortalContext.ResultValueItem = await global::Sidvall.LogManager.SystemContext.Current.DataPortal.LogScopeItemManager.ExecuteAsync(dataPortalContext);
                    OnAfterExecute(dataPortalContext);
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalInvokeComplete(dataPortalContext);
                    await dataPortalContext.DoTaskAsync();
                    message = this.Request.CreateResponse(HttpStatusCode.OK, dataPortalContext.ResultValueItem);
                }
                catch (System.Exception ex)
                {
                    await Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.LogAsync(ex);
                    message = Sidvall.LogManager.Web.SystemContext.Current.ControllerManager.GetResponseMessage(this, ex);
                    dataPortalContext.Exception = ex;
                    global::Sidvall.LogManager.Web.Controllers.Api.V1.ControllerEventManager.DataPortalException(dataPortalContext);
                    await dataPortalContext.DoTaskAsync(false);
                }
            } while (dataPortalContext.DoRetry());
            return message;
        }

        #endregion
    }
}